package power.game.sixtysix.logic;

import power.game.sixtysix.entity.Card;
import power.game.sixtysix.entity.CardColor;
import power.game.sixtysix.entity.GameState;
import power.game.sixtysix.entity.Player;
import power.game.sixtysix.entity.Stack;

public class GamePlay {

	private Player player1;
	private Player player2;
	private Stack stack;
	private CardColor talon;
	private Round round;
	public GamePlay() {
		player1 = new Player();
		player2 = new Player();
		stack = new Stack();
		for (int i = 0; i < 5; i++) {
			player1.addCardToHand(new Card(stack.popCard()));
		}
		for (int i = 0; i < 5; i++) {
			player2.addCardToHand(new Card(stack.popCard()));
		}
		talon = stack.getTalonCard().getCardColor();
		round = new Round(talon);
	}
	
	public Card[] getPlayer1Cards() {
		return player1.getCardsFromHand();
	}
	
	public Card[] getPlayer2Cards() {
		return player2.getCardsFromHand();
	}
	
	public Card getTalon() {
		return stack.getTalonCard();
	}

	public Card player1PutCard(int cardIndex) {
		Card c = player1.removeCardFromHand(cardIndex);
		round.putCard(1, c);
		return c;
	}
	
	public Card player2PutCard(int cardIndex) {
		Card c = player2.removeCardFromHand(cardIndex);
		round.putCard(2, c);
		return c;
	}
	
	public void player1ShowTwenty() {
		player1.addTwenty();
	}
	
	public void player2ShowTwenty() {
		player2.addTwenty();
	}

	public void player1ShowFourty() {
		player1.addFourty();
	}
	
	public void player2ShowFourty() {
		player2.addFourty();
	}
	
	private int lastWinner;
	public int newRound() {
		int roundWinner = round.getWinner();
		if (roundWinner == 1) {
			player1.saveCards(round.getFirstCard(), round.getSecondCard());
		} else {
			player2.saveCards(round.getFirstCard(), round.getSecondCard());
		}
		if (!stack.isEmpty()) {
			if (roundWinner == 1) {
				player1.addCardToHand(new Card(stack.popCard()));
				player2.addCardToHand(new Card(stack.popCard()));
			} else {
				player2.addCardToHand(new Card(stack.popCard()));
				player1.addCardToHand(new Card(stack.popCard()));
			}
		}
		System.out.println("Player1 sum: " + player1.getCardSum());
		System.out.println("Player2 sum: " + player2.getCardSum());
		round = new Round(talon);
		lastWinner = roundWinner;
		return roundWinner;
	}

	public GameState getGameState() {
		if (player1.getCardSum() >= 66) {
			if (player2.getCardSum() == 0) {
				return GameState.Player1_3;
			} else if (player2.getCardSum() < 33) {
				return GameState.Player1_2;
			} else {
				return GameState.Player1_1;
			}
		} else if (player2.getCardSum() >= 66) {
			if (player1.getCardSum() == 0) {
				return GameState.Player2_3;
			} else if (player1.getCardSum() < 33) {
				return GameState.Player2_2;
			} else {
				return GameState.Player2_1;
			}
		} else if (stack.isEmpty() && player1.getCardsFromHand().length == 0) {
			if (lastWinner == 1) {
				return GameState.Player1_1;
			} else {
				return GameState.Player2_1;
			}
		} 
		return GameState.Ongoing;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Player1: ");
		sb.append(player1);
		sb.append("\nPlayer2: ");
		sb.append(player2);
		sb.append("\nStack: ");
		sb.append(stack);
		sb.append("\nTalon: ");
		sb.append(stack.getTalonCard());
		return sb.toString();
	}
	
}
